package com.pandora.credit.web;

import com.pandora.credit.dto.request.CustomerReportRequest;
import com.pandora.credit.dto.response.CustomerReportResponse;
import com.pandora.credit.dto.response.H5ReportResponse;
import com.pandora.credit.exception.AbstractOperateResult;
import com.pandora.credit.exception.OperateSuccess;
import com.pandora.credit.mongo.service.H5ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @author developer
 */
@RestController
public class H5ReportController {
    @Autowired
    private H5ReportService h5ReportService;

    @RequestMapping(value = "/reports", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public AbstractOperateResult receiveH5Report(@RequestBody String report,
                                                 @RequestParam(value = "type") String type) {
        h5ReportService.saveH5Report(report, type);
        return new OperateSuccess(200);
    }


    @RequestMapping(value = "/reports/{reportId}/data", method = RequestMethod.GET,
            produces = {"application/json; charset=utf-8"})
    public H5ReportResponse getH5Report(@PathVariable("reportId") String reportId) {
        H5ReportResponse h5ReportResponse = h5ReportService.queryH5Report(reportId);
        return h5ReportResponse;
    }

    @RequestMapping(value = "/customers/{cust_id}/reports", method = RequestMethod.GET,
            produces = "application/json; charset=utf-8")
    public CustomerReportResponse getCustomerReports(@PathVariable("cust_id") String custId,
                                                     @RequestParam(value = "length", required = false, defaultValue = "") String length,
                                                     @RequestParam(value = "next_id", required = false, defaultValue = "") String nextId,
                                                     @RequestParam(value = "type", required = false, defaultValue = "") String type,
                                                     @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken) {
        CustomerReportRequest request = new CustomerReportRequest(custId, length, type, nextId, authorizationToken);
        CustomerReportResponse customerReport = h5ReportService.queryCustomerReports(request);
        return customerReport;
    }

}
